<?php

/**
 * vend actions.
 *
 * @package    tinystockmis
 * @subpackage vend
 * @author     Linfeng Wu
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class vendActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
    /*$this->sys_vends = Doctrine::getTable('SysVend')
      ->createQuery('a')
      ->execute();
     *
     */
      
  }

  public function executeEditIndex(sfWebRequest $request)
  {
      
  }

  public function executeGetVendList(sfWebRequest $request)
  {
      $this->pager = new sfDoctrinePager(
              "SysVend",
              $request->getParameter("rows", sfConfig::get("app_max_vend_per_page", 10))
          );
      $this->pager->setQuery(Doctrine::getTable("SysVend")->getSysVendQuery());
      $this->pager->setPage($request->getParameter("page", 1));
      $this->pager->init();
      
  }

  public function executeNew(sfWebRequest $request)
  {
    $this->form = new SysVendForm();
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST));

    $this->form = new SysVendForm();

    $this->processForm($request, $this->form);

    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($sys_vend = Doctrine::getTable('SysVend')->find(array($request->getParameter('id'))), sprintf('Object sys_vend does not exist (%s).', $request->getParameter('id')));
    $this->form = new SysVendForm($sys_vend);
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
    $this->forward404Unless($sys_vend = Doctrine::getTable('SysVend')->find(array($request->getParameter('id'))), sprintf('Object sys_vend does not exist (%s).', $request->getParameter('id')));
    $this->form = new SysVendForm($sys_vend);

    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $this->forward404Unless($sys_vend = Doctrine::getTable('SysVend')->find(array($request->getParameter('id'))), sprintf('Object sys_vend does not exist (%s).', $request->getParameter('id')));
    $sys_vend->delete();

    $this->redirect('vend/index');
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $sys_vend = $form->save();

      $this->redirect('vend/edit?id='.$sys_vend->getId());
    }
  }
}
